package com.example.csetpp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.csetpp.exception.GetAllUsersException;
import com.example.csetpp.exception.OssFileOperateException;
import com.example.csetpp.mapper.ProblemMapper;
import com.example.csetpp.mapper.UserMapper;
import com.example.csetpp.pojo.dto.PageDTO;
import com.example.csetpp.pojo.dto.PageUserDTO;
import com.example.csetpp.pojo.dto.RecordId;
import com.example.csetpp.pojo.dto.problem.ProblemSearchDTO;
import com.example.csetpp.pojo.dto.problem.ResponseProblemDTO;
import com.example.csetpp.pojo.entity.Problem;
import com.example.csetpp.pojo.entity.User;
import com.example.csetpp.pojo.vo.problem.PageProblemVO;
import com.example.csetpp.service.ProblemService;
import com.example.csetpp.utils.SnowFlakeID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProblemImpl implements ProblemService {
    @Autowired
    private ProblemMapper problemMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void submitQuestionFeedback(Problem problem) {
        User user = userMapper.selectById(problem.getCustomerId());
        if (user == null) {
            throw new GetAllUsersException("用户不存在");
        }
        problem.setId(SnowFlakeID.getSnowflakeId());
        problem.setSubmitTime(System.currentTimeMillis());
        problem.setStatus(0);
        problemMapper.insert(problem);
    }

    @Override
    public Object getUserFeedback(PageUserDTO pageUserDTO) {
        Page<Problem> page = new Page<>(pageUserDTO.getPage(), pageUserDTO.getSize());
        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", pageUserDTO.getUserId());
        queryWrapper.orderByDesc("submit_time");
        Long i = problemMapper.selectCount(queryWrapper);
        page = problemMapper.selectPage(page, queryWrapper);
        return new PageProblemVO(i, page.getRecords());
    }

    @Override
    public Object getProcessedFeedback(ProblemSearchDTO problemSearchDTO) {
//        Page<Problem> page = new Page<>(pageDTO.getPage(),pageDTO.getSize());
//        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status",1);
//        queryWrapper.orderByDesc("submit_time");
//        Long i = problemMapper.selectCount(queryWrapper);
//        page = problemMapper.selectPage(page,queryWrapper);
//        return new PageProblemVO(i,page.getRecords());
        String userId = problemSearchDTO.getUserId();
        String title = problemSearchDTO.getTitle();
        Long startDate = problemSearchDTO.getStartDate();
        Long endDate = problemSearchDTO.getEndDate();
        Page<Problem> page = new Page<>(problemSearchDTO.getPage(), problemSearchDTO.getSize());
        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userId != null, "customer_id", userId)
                .like(title != null, "title", title)
                .ge(startDate != null, "submit_time", startDate)
                .le(endDate != null, "submit_time", endDate)
                .eq("status", 1);
        queryWrapper.orderByDesc("submit_time");

        Long i = problemMapper.selectCount(queryWrapper);
        page = problemMapper.selectPage(page, queryWrapper);
        return new PageProblemVO(i, page.getRecords());
    }

    @Override
    public Object getUnprocessedFeedback(ProblemSearchDTO problemSearchDTO) {
//        Page<Problem> page = new Page<>(pageDTO.getPage(),pageDTO.getSize());
//        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status",0);
//        queryWrapper.orderByDesc("submit_time");
//        Long i = problemMapper.selectCount(queryWrapper);
//        page = problemMapper.selectPage(page,queryWrapper);
//        return new PageProblemVO(i,page.getRecords());

        String userId = problemSearchDTO.getUserId();
        String title = problemSearchDTO.getTitle();
        Long startDate = problemSearchDTO.getStartDate();
        Long endDate = problemSearchDTO.getEndDate();
        Page<Problem> page = new Page<>(problemSearchDTO.getPage(), problemSearchDTO.getSize());
        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userId != null, "customer_id", userId)
                .like(title != null, "title", title)
                .ge(startDate != null, "submit_time", startDate)
                .le(endDate != null, "submit_time", endDate)
                .eq("status", 0);
        queryWrapper.orderByDesc("submit_time");

        Long i = problemMapper.selectCount(queryWrapper);
        page = problemMapper.selectPage(page, queryWrapper);
        return new PageProblemVO(i, page.getRecords());
    }

    @Override
    public void responseFeedback(ResponseProblemDTO responseProblemDTO) {
        Problem problem = problemMapper.selectById(responseProblemDTO.getId());
        if (problem == null) {
            throw new OssFileOperateException("问题记录不存在！");
        }
        problem.setResponse(responseProblemDTO.getResponse());
        problem.setResponseTime(System.currentTimeMillis());
        problem.setStatus(1);
        problemMapper.updateById(problem);
    }

    @Override
    public void deleteQuestionFeedback(RecordId recordId) {
        problemMapper.deleteById(recordId.getId());
    }

    @Override
    public Object searchQuestionFeedback(ProblemSearchDTO problemSearchDTO) {
        String userId = problemSearchDTO.getUserId();
        String title = problemSearchDTO.getTitle();
        Long startDate = problemSearchDTO.getStartDate();
        Long endDate = problemSearchDTO.getEndDate();
        Page<Problem> page = new Page<>(problemSearchDTO.getPage(), problemSearchDTO.getSize());
        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userId != null, "customer_id", userId)
                .like(title != null, "title", title)
                .ge(startDate != null, "submit_time", startDate)
                .le(endDate != null, "submit_time", endDate);

        queryWrapper.orderByDesc("submit_time");
        Long i = problemMapper.selectCount(queryWrapper);
        page = problemMapper.selectPage(page, queryWrapper);
        return new PageProblemVO(i, page.getRecords());
    }
}
